@extends('layouts._app')
@section('content')
    <link rel="stylesheet" href="{{asset('static/ace/assets/css/ace.min.css')}}"/>
    <style>
        html {
            background-color: #fff;
        }
    </style>
    <body>
    <div class="page-content-wrap" id="table-list" style="background-color: #fff;">
        @include('layouts._flash')
        <div class="layui-form-item">
            {!! Form::open(['class'=>'layui-form','method'=>'get']) !!}
            <div class="layui-inline tool-btn">
                <a class="layui-btn layui-btn-small layui-btn-primary" id='email-refresh-btn'
                   href="{{route('blog.category.index')}}" data-title='刷新'>
                    <i class="layui-icon">&#x1002;</i>
                </a>
                <button class="layui-btn layui-btn-small layui-btn-normal addBtn hidden-xs"
                        data-title="分类添加"
                        data-url="{{route('blog.category.create')}}?static={{$request['static']}}"><i
                        class="layui-icon">&#xe654;</i></button>
            </div>
            <div class="layui-inline">
                <select lay-filter="static">
                    <option value="1" @if($request['static'] == 1) selected @endif>文章</option>
                    <option value="2" @if($request['static'] == 2) selected @endif>意见反馈</option>
                    <option value="3" @if($request['static'] == 3) selected @endif>问题分类</option>
                    <option value="4" @if($request['static'] == 4) selected @endif>图片集锦</option>
                </select>
            </div>
            <div class="layui-input-inline" style="float: right;">
                <select id="change_category" lay-filter="category" lay-search data-id="categories">
                    <option selected>下载</option>
                    <option>.txt</option>
                    <option>.xlsx</option>
                </select>
            </div>
            {!! Form::close(); !!}
        </div>
        <div class="dd" id="nestable" style="max-width: 900px;">
            <ol class="dd-list" id="dd-list">
                @foreach($category as $value)
                    <li class="dd-item dd2-item" data-id="{{$value->id}}">
                        <div class="dd-handle dd2-handle">
                            <i class="fa fa-arrows"></i>
                        </div>
                        <div class="dd2-content">
                            {{$value->name}}
                            <div class="pull-right action-buttons">
                                <a class="layui-btn layui-btn-mini layui-btn-normal"
                                   href="{{route('blog.category.status',$value->id)}}"
                                   data-method="delete" title="@if($value->is_show == 1) 禁用 @else 启用 @endif"
                                   data-token="{{csrf_token()}}"
                                   data-confirm="@if($value->is_show == 1) 确认禁用? @else 确认启用? @endif">
                                    <i class="@if($value->is_show == 1) fa fa-check @else fa fa-times-circle-o @endif"></i>
                                </a>
                                <button
                                    class="layui-btn layui-btn-mini layui-btn-normal  add-btn"
                                    data-id="{{$value->id}}" data-title="分类添加"
                                    data-url="{{route('blog.category.create')}}?id={{$value->id}}&static={{$request['static']}}">
                                    <i class="layui-icon">&#xe654;</i></button>
                                <button class="layui-btn layui-btn-mini layui-btn-normal  edit-btn"
                                        data-id="{{$value->id}}" data-title="分类编辑"
                                        data-url="{{route('blog.category.edit',['id'=>$value->id,'static'=>$request['static']])}}">
                                    <i
                                        class="layui-icon">&#xe642;</i></button>
                                @if($value->children->isEmpty())
                                    <a class="layui-btn layui-btn-mini layui-btn-danger"
                                       href="{{route('blog.category.destroy',$value->id)}}"
                                       data-method="delete"
                                       data-token="{{csrf_token()}}" data-confirm="确认删除吗?"><i
                                            class="layui-icon">&#xe640;</i></a>
                                @endif
                            </div>
                        </div>
                        @if(!$value->children->isEmpty())
                            <ol class="dd-list">
                                @foreach($value->children as $v)
                                    <li class="dd-item dd2-item" data-id="{{$v->id}}">
                                        <div class="dd-handle dd2-handle">
                                            <i class="fa fa-arrows"></i>
                                        </div>
                                        <div class="dd2-content">
                                            {{$v->name}}
                                            <div class="pull-right action-buttons">
                                                <a class="layui-btn layui-btn-mini layui-btn-normal"
                                                   href="{{route('blog.category.status',$value->id)}}"
                                                   data-method="delete"
                                                   title="@if($value->is_show == 1) 禁用 @else 启用 @endif"
                                                   data-token="{{csrf_token()}}"
                                                   data-confirm="@if($value->is_show == 1) 确认禁用? @else 确认启用? @endif">
                                                    <i class="@if($value->is_show == 1) fa fa-check @else fa fa-times-circle-o @endif"></i>
                                                </a>
                                                <button class="layui-btn layui-btn-mini layui-btn-normal  edit-btn"
                                                        data-id="{{$v->id}}" data-title="分类编辑"
                                                        data-url="{{route('blog.category.edit',['id'=>$v->id,'static'=>$request['static']])}}">
                                                    <i
                                                        class="layui-icon">&#xe642;</i></button>
                                                @if($v->children->isEmpty())
                                                    <a class="layui-btn layui-btn-mini layui-btn-danger"
                                                       href="{{route('blog.category.destroy',$v->id)}}"
                                                       data-method="delete"
                                                       data-token="{{csrf_token()}}" data-confirm="确认删除吗?"><i
                                                            class="layui-icon">&#xe640;</i></a>
                                                @endif
                                            </div>
                                        </div>
                                    </li>
                                @endforeach
                            </ol>
                        @endif
                    </li>
                @endforeach
            </ol>
        </div>
    </div>
    <script src="{{asset('static/admin/layui/layui.js')}}" type="text/javascript" charset="utf-8"></script>
    <script src="{{asset('static/admin/js/common.js')}}" type="text/javascript" charset="utf-8"></script>
    <script src="{{asset('js/jquery.min.js')}}" type="text/javascript" charset="utf-8"></script>
    <script src="{{asset('js/destroy.js')}}" type="text/javascript" charset="utf-8"></script>
    <script src="{{asset('js/common.js')}}" type="text/javascript" charset="utf-8"></script>
    <script src="{{asset('static/ace/assets/js/bootstrap.min.js')}}"></script>
    <script src="{{asset('static/ace/assets/js/jquery.nestable.min.js')}}"></script>
    <script>
        /**
         * 排序功能
         */
        jQuery(function ($) {
            $('.dd-handle a').on('mousedown', function (e) {
                e.stopPropagation();
            });
            $('[data-rel="tooltip"]').tooltip();
            var dd = $('.dd');
            dd.nestable('expandAll');//展开所有节点
            dd.nestable('collapseAll');//折叠所有节点
            dd.nestable().on('change', function () {
                var r = dd.nestable('serialize');
                var sort = JSON.stringify(r);    //改变排序之后的数据
                $.ajax({
                    type: 'POST',
                    url: '{{route('blog.category.sort')}}',
                    data: {sort: sort, filter: true},
                    dataType: "json",
                    beforeSend: function () {
                        layer.load(1, {
                            shade: [0.1, '#333333'] //0.1透明度的白色背景
                        });
                    },
                    error: function (xhr) {//获取ajax的错误信息
                        layer.alert(xhr.responseText, "Failed", function () {
                            layer.closeAll();
                        });
                    },
                    complete: function (XHR) {
                        XHR = null;
                    },
                    success: function (msg) {
                        layer.closeAll();
                        if (msg.code == 403) {
                            layer.msg(msg.msg, {icon: 4, time: 3000}, function () {
                                location.reload();
                            });
                        }
                        if (msg.code != 200) {
                            layer.msg(msg.msg, {icon: 5, time: 3000}, function () {
                                location.reload();
                            });
                        }
                        layer.msg(msg.msg, {icon: 1, time: 1000}, function () {
                            location.reload();
                        });
                    }
                })
            });

        });
        layui.use(['jquery', 'dialog', 'form'], function () {
            var $ = layui.jquery;
            var dialog = layui.dialog;
            var form = layui.form();

            form.on('select(category)', function (e) {
                if (e.value == '下载') {
                    return false;
                }
                /**
                 * 写入文件下载
                 * @param id
                 */
                var table = $("#change_category").data('id');
                var ext = e.value;
                dialog.confirm({
                    message: '这可能会耗费很长时间！是否继续',
                    success: function () {
                        window.open('{{route('TextFile')}}' + '?table=' + table + '&ext=' + ext, '_parent');
                        window.location.reload();
                    },
                });
            });

            form.on('select(static)', function (e) {
                window.location.href = '{{route('blog.category.index')}}?static=' + e.value;
            });

            form.render();
        });

    </script>
    </body>
@endsection
